package com.service.impl;

import com.alibaba.excel.EasyExcel;
import com.dao.KitchenSupervisorDao;
import com.entity.BlanketOrder;
import com.entity.excelEntity.SummaryOrder;
import com.entity.excelEntity.UserDailyOrder;
import com.service.KitchenSupervisorService;
import com.utils.CurrentTime;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class KitchenSupervisorServiceImpl implements KitchenSupervisorService {

    @Autowired
    private KitchenSupervisorDao kitchenSupervisorDao;

    @Override
    public void getAllOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        log.info("getAllOrder.....");
        log.info("downloadExcel...");

        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");

        // 设置防止中文名乱码
        String filename = URLEncoder.encode(CurrentTime.currentTime().substring(0, 10) + "总括订单", "utf-8");
        // 文件下载方式(附件下载)
        response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xlsx");

        // 构建 Excel 数据
        List<SummaryOrder> userExcels = new ArrayList<>();
        List<SummaryOrder> orders = kitchenSupervisorDao.getSummaryDownloadExcel();
        for (SummaryOrder order : orders) {
            userExcels.add(new SummaryOrder(
                    order.getCaipinmingcheng(),
                    "份",
                    order.getXiadanshuliang()
            ));
        }

        // 写入数据到 Excel 文件并通过输出流返回
        EasyExcel.write(response.getOutputStream(), SummaryOrder.class)
                .sheet("订单详情")
                .doWrite(userExcels);

        log.info("---------------serviceImpl...");
    }



    @Override
    public List<BlanketOrder> getAllPersonalOrder(String userName) {
        log.info("getAllPersonalOrder.....");
        List<BlanketOrder> blanketOrderList = kitchenSupervisorDao.getAllPersonalOrder(userName);
        //return blanketOrderList;
        return null;
    }

    @Override
    public void downloadExcel(HttpServletRequest request, HttpServletResponse response, String userName) throws IOException {
        log.info("downloadExcel...");
        // 设置响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 设置防止中文名乱码
        String filename = URLEncoder.encode(CurrentTime.currentTime().substring(0, 10) + "各员工订单", "utf-8");
        // 文件下载方式(附件下载)
        response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xlsx");

        // 构建 Excel 数据
        List<UserDailyOrder> userExcels = new ArrayList<>();
        List<UserDailyOrder> orders = kitchenSupervisorDao.getDownloadExcel();
        for (UserDailyOrder order : orders) {
            userExcels.add(new UserDailyOrder(order.getYonghuming(),
                    order.getLianxidianhua(),
                    order.getCanzhuohao(),
                    order.getCaipinmingcheng(),
                    "份",
                    order.getXiadanshuliang(),
                    userName,
                    CurrentTime.currentTime()));
        }

        // 写入数据到 Excel 文件并通过输出流返回
        EasyExcel.write(response.getOutputStream(), UserDailyOrder.class)
                .sheet("订单详情")
                .doWrite(userExcels);

        log.info("---------------serviceImpl...");



    }
}
